<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:10pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f9f9f9;"><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333; background-color:#f9f9f9;">Если же необходимо удалить папку полностью, а не только файлы в ней, то лучше использовать такой код:</span></p>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:32px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Tinos,Arial,serif'; font-size:18px; color:#110000; background-color:#f9f9f9;"><br /></p>
<table border="0" style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;" cellspacing="2" cellpadding="0">
<tr>
<td bgcolor="transparent" style=" padding-left:0; padding-right:0; padding-top:0; padding-bottom:0;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> RemoveFolderWithContent()</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">Dim</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> sFolder </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">As</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#f660ab;">String</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">, sFiles </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">As</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#f660ab;">String</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'диалог запроса выбора папки на удаление</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">With</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> Application.FileDialog(msoFileDialogFolderPicker)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">If</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> .Show = </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#00c2ff;">False</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Then</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Exit</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        sFolder = .SelectedItems(1)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">End</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">With</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    sFolder = sFolder &amp; IIf(Right(sFolder, 1) = Application.PathSeparator, </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">, Application.PathSeparator)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'путь к папке можно задать статично, если он заранее известен и не изменяется</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'    sFolder = &quot;C:\temp\Ежедневные отчеты\10072017&quot; 'путь к папке, которую надо удалить</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    Shell </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;cmd /c rd /S/Q &quot;&quot;&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> &amp; sFolder &amp; </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;&quot;&quot;&quot;</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">End</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span></p></td></tr></table>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333; background-color:#f9f9f9;">Этот код удалить папку вместе со всеми файлами буквально за секунду.</span><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333;"><br /></span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">Вообще в VBA есть специальная команда для удаления директорий(папок) </span><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:14px; font-weight:296; color:#262626; background-color:#f0f0f0;">RmDir</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">. Но она способна удалить только пустую папку, поэтому редко когда можно найти её практическое применение. Если в файле есть хоть один файл то команда </span><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:14px; font-weight:296; color:#262626; background-color:#f0f0f0;">RmDir</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;"> выдаст ошибку </span><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:14px; font-weight:296; color:#262626; background-color:#f0f0f0;">'75' - File/Path access error</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">.</span></p>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333;"><br /></p>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333;"><br /></p>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333;"><br /></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:xx-large; font-weight:696; color:#a7737d; background-color:#f9f9f9; text-transform:uppercase;">КАК УДАЛИТЬ ПАПКУ ИЛИ ВСЕ ФАЙЛЫ ИЗ ПАПКИ ЧЕРЕЗ VBA</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f9f9f9;"><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333; background-color:#f9f9f9;">Предположим, что ежедневно во временную папку поступают файлы отчетов от филиалов. Они могут собираться из почты кодом вроде такого:</span><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333;"> </span><a href="https://www.excel-vba.ru/chto-umeet-excel/soxranit-vlozheniya-iz-outlook-v-papku/"><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; text-decoration: underline; color:#152d4d;">Сохранить вложения из Outlook в указанную папку</span></a><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333;"> или добавляться в папку иными средствами. Далее Вы собираете данные из этих файлов неким кодом(вроде этого - </span><a href="https://www.excel-vba.ru/chto-umeet-excel/kak-sobrat-dannye-s-neskolkix-listov-ili-knig/"><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; text-decoration: underline; color:#152d4d;">Как собрать данные с нескольких листов или книг?</span></a><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333;">). Но с каждым днем файлов все больше и больше и приходится заходить в папку и руками чистить её от лишних файлов, чтобы при сборе данных не приходилось каждый раз искать и отбирать только новые файлы.<br />Если надо удалять только конкретные файлы(например только файлы Excel, содержащие в имени слово &quot;отчет&quot;), то можно использовать такой код:</span></p>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:32px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Tinos,Arial,serif'; font-size:18px; color:#110000; background-color:#f9f9f9;"><br /></p>
<table border="0" style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;" cellspacing="2" cellpadding="0">
<tr>
<td bgcolor="transparent" style=" padding-left:0; padding-right:0; padding-top:0; padding-bottom:0;">
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> Remove_AllFilesFromFolder()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">Dim</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> sFolder </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">As</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#f660ab;">String</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">, sFiles </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">As</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#f660ab;">String</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'диалог запроса выбора папки с файлами</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'подробнее про диалоги выбора папки или файла:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'       http://www.excel-vba.ru/chto-umeet-excel/dialogovoe-okno-vybora-fajlovpapki/</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">With</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> Application.FileDialog(msoFileDialogFolderPicker)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">If</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> .Show = </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#00c2ff;">False</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Then</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Exit</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        sFolder = .SelectedItems(1)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">End</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">With</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    sFolder = sFolder &amp; IIf(Right(sFolder, 1) = Application.PathSeparator, </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">, Application.PathSeparator)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'отбирать только файлы Excel, содержащие в имени слово &quot;отчет&quot;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    sFiles = Dir(sFolder &amp; </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;*отчет*.xls*&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'цикл по всем файлам в папке</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">On</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">Error</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Resume</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Next</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Do</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">While</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> sFiles &lt;&gt; </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;&quot;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'удаляем файл</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        Kill sFolder &amp; sFiles</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">If</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> Err.Number = 70 </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Then</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">            MsgBox </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;Невозможно удалить файл '&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> &amp; sFiles &amp; </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;'. Возможно файл открыт в другой программе или нет прав на удаление&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">, vbCritical, </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;www.excel-vba.ru&quot;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">            Err.Clear</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">End</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">If</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'на всякий случай передаем управление системе,</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'чтобы дождаться удаления</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">DoEvents</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'получаем имя следующего файла в папке</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        sFiles = Dir</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Loop</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">End</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span></pre></td></tr></table>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f9f9f9;"><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; font-weight:600; text-decoration: underline; color:#2c5ea1; background-color:#f0f0f0;">Как использовать:</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;"> Для начала надо убедиться, что разрешены макросы и при необходимости включить их: </span><a href="https://www.excel-vba.ru/chto-umeet-excel/pochemu-ne-rabotaet-makros/"><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; text-decoration: underline; color:#152d4d; background-color:#f0f0f0;">почему не работает макрос</span></a><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">. Затем копируем код выше, из Excel переходим в редактор VBA(</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; font-weight:600; color:#333333; background-color:#ffffff;">Alt</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">+</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; font-weight:600; color:#333333; background-color:#ffffff;">F11</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">) -</span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#333333; background-color:#f0f0f0;">Insert</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;"> -</span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#333333; background-color:#f0f0f0;">Module</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">. Вставляем туда скопированный код. Теперь код можно вызывать нажатием клавиш </span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; font-weight:600; color:#333333; background-color:#ffffff;">Alt</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">+</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; font-weight:600; color:#333333; background-color:#ffffff;">F8</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;"> -выделяем </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#262626; background-color:#f0f0f0;">Remove_AllFilesFromFolder</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;"> -</span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#333333; background-color:#f0f0f0;">Выполнить</span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#808080; background-color:#f0f0f0;">(Run)</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">.</span><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333;"><br />Чтобы удалять полностью </span><span style=" font-family:'inherit'; font-size:18px; font-weight:600; color:#333333;">все</span><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333;"> файлы в папке(а не только файлы Excel), а саму папку оставить, то строку </span><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:16px; font-weight:296; color:#262626;">sFiles = Dir(sFolder &amp; &quot;*отчет*.xls*&quot;)</span><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333;"> надо записать так: </span><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:16px; font-weight:296; color:#262626;">sFiles = Dir(sFolder &amp; &quot;*&quot;)</span></p>
<hr />
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f9f9f9;"><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333; background-color:#f9f9f9;">Если необходимо удалять файлы по дате создания/изменения(например, только файлы, созданные раньше 01.03.2017), то можно использовать такой код:</span></p>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:32px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Tinos,Arial,serif'; font-size:18px; color:#110000; background-color:#f9f9f9;"><br /></p>
<table border="0" style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;" cellspacing="2" cellpadding="0">
<tr>
<td bgcolor="transparent" style=" padding-left:0; padding-right:0; padding-top:0; padding-bottom:0;">
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> Remove_FilesFromFolder_AfterDate()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">Dim</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> sFolder </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">As</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#f660ab;">String</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">, sFiles </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">As</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#f660ab;">String</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">Dim</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> dd </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">As</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#f660ab;">Date</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">, dKill </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">As</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#f660ab;">Date</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'задаем дату. Если файл был создан/изменен до этой даты - он будет удален</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    dKill = CDate(</span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;01.03.2017&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">) </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'можно задать проще: dKill = #3/1/2017#</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'диалог запроса выбора папки с файлами</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'подробнее про диалоги выбора папки или файла:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'       http://www.excel-vba.ru/chto-umeet-excel/dialogovoe-okno-vybora-fajlovpapki/</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">With</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> Application.FileDialog(msoFileDialogFolderPicker)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">If</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> .Show = </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#00c2ff;">False</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Then</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Exit</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        sFolder = .SelectedItems(1)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">End</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">With</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    sFolder = sFolder &amp; IIf(Right(sFolder, 1) = Application.PathSeparator, </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">, Application.PathSeparator)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    sFiles = Dir(sFolder &amp; </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;*&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'цикл по всем файлам в папке</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">On</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">Error</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Resume</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Next</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Do</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">While</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> sFiles &lt;&gt; </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;&quot;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'получаем дату создания или изменения файла</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        dd = FileDateTime(sFolder &amp; sFiles)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'если дата файла меньше заданной для удаления(был создан раньше)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">If</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> dd &lt; dKill </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Then</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">            </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'удаляем файл</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">            Kill sFolder &amp; sFiles</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">            </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">If</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> Err.Number = 70 </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Then</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">                MsgBox </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;Невозможно удалить файл '&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> &amp; sFiles &amp; </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;'. Возможно файл открыт в другой программе или нет прав на удаление&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">, vbCritical, </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;www.excel-vba.ru&quot;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">                Err.Clear</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">            </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">End</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">If</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">            </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'на всякий случай передаем управление системе,</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">            </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'чтобы дождаться удаления</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">            </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">DoEvents</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">End</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">If</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'получаем имя следующего файла в папке</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        sFiles = Dir</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Loop</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">End</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span></pre></td></tr></table>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f9f9f9;"><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333; background-color:#f9f9f9;">Если необходимо всегда удалять файлы, дата создания которых раньше текущей, то строку</span><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333;"> </span><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:16px; font-weight:296; color:#262626;">dKill = CDate(&quot;01.03.2017&quot;)</span><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333;"> нужно заменить на такую: </span><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:16px; font-weight:296; color:#262626;">dKill = Date</span><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333;">. Если удалить надо файлы недельной давности, то: </span><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:16px; font-weight:296; color:#262626;">dKill = Date-7</span></p>
<hr />
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f9f9f9;"><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333; background-color:#f9f9f9;">Если же необходимо удалить папку полностью, а не только файлы в ней, то лучше использовать такой код:</span></p>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:32px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Tinos,Arial,serif'; font-size:18px; color:#110000; background-color:#f9f9f9;"><br /></p>
<table border="0" style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;" cellspacing="2" cellpadding="0">
<tr>
<td bgcolor="transparent" style=" padding-left:0; padding-right:0; padding-top:0; padding-bottom:0;">
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> RemoveFolderWithContent()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">Dim</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> sFolder </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">As</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#f660ab;">String</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">, sFiles </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#151b8d;">As</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#f660ab;">String</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'диалог запроса выбора папки на удаление</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">With</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> Application.FileDialog(msoFileDialogFolderPicker)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">If</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> .Show = </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#00c2ff;">False</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">Then</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Exit</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">        sFolder = .SelectedItems(1)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">End</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">With</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    sFolder = sFolder &amp; IIf(Right(sFolder, 1) = Application.PathSeparator, </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;">, Application.PathSeparator)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'путь к папке можно задать статично, если он заранее известен и не изменяется</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    </span><span style=" font-family:'monospace'; font-size:14px; color:#008000;">'    sFolder = &quot;C:\temp\Ежедневные отчеты\10072017&quot; 'путь к папке, которую надо удалить</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'monospace'; font-size:14px; color:#222222;">    Shell </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;cmd /c rd /S/Q &quot;&quot;&quot;</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> &amp; sFolder &amp; </span><span style=" font-family:'monospace'; font-size:14px; color:#800000;">&quot;&quot;&quot;&quot;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; line-height:16px; background-color:transparent;"><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#8d38c9;">End</span><span style=" font-family:'monospace'; font-size:14px; color:#222222;"> </span><span style=" font-family:'inherit'; font-size:14px; font-weight:600; color:#e56717;">Sub</span></pre></td></tr></table>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f9f9f9;"><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333; background-color:#f9f9f9;">Этот код удалить папку вместе со всеми файлами буквально за секунду.</span><span style=" font-family:'Tinos,Arial,serif'; font-size:18px; color:#333333;"><br /></span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">Вообще в VBA есть специальная команда для удаления директорий(папок) </span><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:14px; font-weight:296; color:#262626; background-color:#f0f0f0;">RmDir</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">. Но она способна удалить только пустую папку, поэтому редко когда можно найти её практическое применение. Если в файле есть хоть один файл то команда </span><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:14px; font-weight:296; color:#262626; background-color:#f0f0f0;">RmDir</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;"> выдаст ошибку </span><span style=" font-family:'Roboto Condensed,sans-serif'; font-size:14px; font-weight:296; color:#262626; background-color:#f0f0f0;">'75' - File/Path access error</span><span style=" font-family:'Tinos,Arial,serif'; font-size:14px; color:#333333; background-color:#f0f0f0;">.</span></p></body></html>